using System;
using UnityEngine;

public class CompetitionButton : MonoBehaviour
{
	public UILabel label;

	private GGServerRequestsBackend.ActiveCompetitionRequest cachedRequest;

	private float timeTillUpdate;

	private float updateDelay = 0.5f;

	private void Awake()
	{
		label.text = "Competition";
	}

	private void OnEnable()
	{
		LoadActiveCompetitionRequest();
	}

	private void RequestComplete(GGServerRequestsBackend.ServerRequest req)
	{
		if (req.status == GGServerRequestsBackend.ServerRequest.RequestStatus.Success)
		{
			cachedRequest = req as GGServerRequestsBackend.ActiveCompetitionRequest;
			UpdateUI();
		}
	}

	private void LoadActiveCompetitionRequest()
	{
		GGServerRequestsBackend instance = BehaviourSingletonInit<GGServerRequestsBackend>.instance;
		GGServerRequestsBackend.ActiveCompetitionRequest activeCompetitionRequest = new GGServerRequestsBackend.ActiveCompetitionRequest();
		activeCompetitionRequest.cache = CacheStategy.GetFromCache;
		instance.GetActiveCompetition(activeCompetitionRequest, RequestComplete);
	}

	private void UpdateUI()
	{
		if (cachedRequest == null || cachedRequest.timeSpanTillEndOfCompetition < TimeSpan.Zero)
		{
			label.text = "League";
		}
		else
		{
			label.text = "League:\n" + GGFormat.FormatTimeSpan(cachedRequest.timeSpanTillEndOfCompetition);
		}
	}

	private void Update()
	{
		timeTillUpdate -= Time.deltaTime;
		if (timeTillUpdate <= 0f)
		{
			timeTillUpdate = updateDelay;
			UpdateUI();
		}
	}

	public void OnClick()
	{
		MainNavigationManager mainNavigationManager = NavigationManager.instance as MainNavigationManager;
		mainNavigationManager.Push(mainNavigationManager.leaderboards);
	}
}
